/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.adgroup_ad_service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.hiddenequity.acts.Act_Super;
import com.hiddenequity.utils.Converter;
import com.hiddenequity.utils.XMLUtils;

public class AdGroupAdResponseParser {
	
	
	Document _XMLDocument;
	
	
	/*
	 * Constructor
	 */
	public AdGroupAdResponseParser(String XMLData){
		try {
			_XMLDocument = XMLUtils.XMLStringToDoc(XMLData);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}//Constructor

	
	
	/*
	 * Check response for errors
	 */
	public String checkForErrors(){
		String error = null;
		try {
	        NodeList faultstring = _XMLDocument.getElementsByTagName("faultstring");
	        error =  XMLUtils.getCharacterDataFromElement((Element) faultstring.item(0));
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }
		return error;
	}//checkForErrors
	
	
	
	/*
	 * Parse ad list data
	 */
	public ArrayList<HashMap<String, String>> getAdList(){
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		
		try {
	        
			Act_Super.resetTotals();
			
			NodeList nodes = _XMLDocument.getElementsByTagName("entries");
	        
	        for (int i = 0; i < nodes.getLength(); i++) {
                //fill up hush map for list population
            	HashMap<String, String> temp = new HashMap<String, String>();

	        	Element element = (Element) nodes.item(i);

	        	NodeList id = element.getElementsByTagName("id");
	        	temp.put("id", XMLUtils.getCharacterDataFromElement((Element) id.item(0)));

	        	NodeList url = element.getElementsByTagName("url");
	        	temp.put("url", XMLUtils.getCharacterDataFromElement((Element) url.item(0)));

	        	NodeList displayUrl = element.getElementsByTagName("displayUrl");
	        	temp.put("displayUrl", XMLUtils.getCharacterDataFromElement((Element) displayUrl.item(0)));
	        	
	        	NodeList approvalStatus = element.getElementsByTagName("approvalStatus");
	        	temp.put("approvalStatus", XMLUtils.getCharacterDataFromElement((Element) approvalStatus.item(0)));        	

	        	NodeList headline = element.getElementsByTagName("headline");
	        	temp.put("headline", XMLUtils.getCharacterDataFromElement((Element) headline.item(0)));   
	        	
	        	NodeList description1 = element.getElementsByTagName("description1");
	        	temp.put("description1", XMLUtils.getCharacterDataFromElement((Element) description1.item(0)));   
	        		        	
	        	NodeList description2 = element.getElementsByTagName("description2");
	        	temp.put("description2", XMLUtils.getCharacterDataFromElement((Element) description2.item(0)));
	        	
	        	NodeList status = element.getElementsByTagName("status");
	        	temp.put("status", XMLUtils.getCharacterDataFromElement((Element) status.item(0)));

	        	NodeList network = element.getElementsByTagName("network");
	        	temp.put("network", XMLUtils.getCharacterDataFromElement((Element) network.item(0)));

	        	NodeList clicks = element.getElementsByTagName("clicks");
	        	temp.put("clicks", XMLUtils.getCharacterDataFromElement((Element) clicks.item(0)));
	
	        	NodeList ctr = element.getElementsByTagName("ctr");
	        	temp.put("ctr", Converter.formatStrNumberPercent(
	        			XMLUtils.getCharacterDataFromElement((Element) ctr.item(0))
	        			));
	        	
	        	NodeList cost = element.getElementsByTagName("cost");
	        	Element costElement = (Element) cost.item(0);
	        	NodeList costAmount = costElement.getElementsByTagName("microAmount");
	        	temp.put("cost", Converter.microamountToStr(
	        			XMLUtils.getCharacterDataFromElement((Element) costAmount.item(0))
	        			));
	        	
	        	NodeList avgCpc = element.getElementsByTagName("averageCpc");
	        	Element avgCpcElement = (Element) avgCpc.item(0);
	        	NodeList avgCpcAmount = avgCpcElement.getElementsByTagName("microAmount");
	        	temp.put("avgcpc", Converter.microamountToStr(
	        			XMLUtils.getCharacterDataFromElement((Element) avgCpcAmount.item(0))
	        			));

	        	
	        	NodeList impressions = element.getElementsByTagName("impressions");
	        	temp.put("impressions", XMLUtils.getCharacterDataFromElement((Element) impressions.item(0)));
	        	
	        	//Predicate to show only text ads
	        	NodeList adType = element.getElementsByTagName("Ad.Type");
	        	String adTypeValue = XMLUtils.getCharacterDataFromElement((Element) adType.item(0));
	        	
	        	if(adTypeValue.equals("TextAd")){
	        		Act_Super.uppendStatsToTotal(temp);
	        		list.add(temp);
	        	}
	        		
	        }
	        
	        Act_Super.calculateTotalStats();
	        
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }

		return list;
	}//getAdList
	

	
	/*
	 * parse ad data
	 */
	public Map<String, String> getAdData(){
		Map<String, String> temp = new HashMap<String, String>();
		
		try {
			NodeList nodes = _XMLDocument.getElementsByTagName("entries");

        	Element element = (Element) nodes.item(0);

        	NodeList url = element.getElementsByTagName("url");
        	temp.put("url", XMLUtils.getCharacterDataFromElement((Element) url.item(0)));

        	NodeList displayUrl = element.getElementsByTagName("displayUrl");
        	temp.put("displayUrl", XMLUtils.getCharacterDataFromElement((Element) displayUrl.item(0)));
        	
        	NodeList approvalStatus = element.getElementsByTagName("approvalStatus");
        	temp.put("approvalStatus", XMLUtils.getCharacterDataFromElement((Element) approvalStatus.item(0)));        	

        	NodeList headline = element.getElementsByTagName("headline");
        	temp.put("headline", XMLUtils.getCharacterDataFromElement((Element) headline.item(0)));   
        	
        	NodeList description1 = element.getElementsByTagName("description1");
        	temp.put("description1", XMLUtils.getCharacterDataFromElement((Element) description1.item(0)));   
        		        	
        	NodeList description2 = element.getElementsByTagName("description2");
        	temp.put("description2", XMLUtils.getCharacterDataFromElement((Element) description2.item(0)));
	   
		} catch (Exception e) {
	    	e.printStackTrace();
	    }

		return temp;		
	}//getAdData
	
	
}
